import { Store } from '../store/store-model.js';
import { Category } from '../category/category-model.js';

var category = new Category();
var store = new Store();

Page({
  data: {
    id: null,
    pageIndex: 1,
    isLoadedAll: false,
    currentMenuIndex: 0,
    loadingHidden: false,
    currentTab: 0,
    transClassArr: ['tanslate0', 'tanslate1', 'tanslate2', 'tanslate3', 'tanslate4', 'tanslate5', 'tanslate6', 'tanslate7', 'tanslate8', 'tanslate9', 'tanslate10', 'tanslate11', 'tanslate12'],
  },
  onLoad: function (options) {
    var id = options.id;
    this.data.id = id;
    this._getStore();
    this._getCategory();
  },

  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = category.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },
  
  _getCategory: function (callback) {
    var that = this;
    store.getCategory(this.data.id, (storeCategory) => {
      that.setData({
        StoreCategory:storeCategory
      });
      that.getProductsByStoreCate(this.data.id,storeCategory[0].id, (data) => {
        var dataObj = {
          products: data,
          topImgUrl: storeCategory[0].img.url,
          title: storeCategory[0].name
        };
        that.setData({
          loadingHidden: true,
          categoryInfo0: dataObj
        });
      }),
      callback && callback();
    });
  },
  _getStore: function (callback) {
    var that = this;
    store.getStore(this.data.id,(res) => {
        that.setData({
          Store:res
        });
      callback && callback();
    });
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //目录
  /*切换分类*/
  changeCategory: function (event) {
    var index = store.getDataSet(event, 'index'),
      id = store.getDataSet(event, 'id'),//获取data-set
      key = store.getDataSet(event,'store')
    this.setData({
      currentMenuIndex: index
    });

    //如果数据是第一次请求
    if (!this.isLoadedData(index)) {
      var that = this;
      this.getProductsByStoreCate(key,id, (data) => {
        that.setData(
          that.getDataObjForBind(index, data)
          );
      });
    }
  },
    isLoadedData: function (index) {
      if (this.data['categoryInfo' + index]) {
        return true;
      }
      return false;
    },

  getDataObjForBind: function (index, data) {
    var obj = {},
      arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
      baseData = this.data.StoreCategory[index];
    for (var item in arr) {
      if (item == arr[index]) {
        obj['categoryInfo' + item] = {
          products: data,
          topImgUrl: baseData.img.url,
          title: baseData.name
        };
        return obj;
      }
    }
  },
   getProductsByStoreCate: function (key,id, callback) {
     store.getProductsByStoreCate(key,id, (data) => {
      callback && callback(data);
    });
  },

})